﻿// Decompiled with JetBrains decompiler
// Type: System.Web.Razor.Generator.HelperCodeGenerator
// Assembly: System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// MVID: 682513D6-F37E-48AC-9909-F3EE103CFF14
// Assembly location: D:\Project\Idea\无人机外网\uav\客户端工程\排查工具\bin\Release\System.Web.Razor.dll

using Microsoft.Internal.Web.Utils;
using System.CodeDom;
using System.Globalization;
using System.Web.Razor.Parser.SyntaxTree;
using System.Web.Razor.Text;

namespace System.Web.Razor.Generator
{
  /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Represents a helper code generator.</summary>
  public class HelperCodeGenerator : BlockCodeGenerator
  {
    private const string HelperWriterName = "__razor_helper_writer";
    private CodeWriter _writer;
    private string _oldWriter;
    private IDisposable _statementCollectorToken;

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Initializes a new instance of the <see cref="T:System.Web.Razor.Generator.HelperCodeGenerator" /> class.</summary>
    /// <param name="signature">The signature.</param>
    /// <param name="headerComplete">true to complete the header; otherwise, false.</param>
    public HelperCodeGenerator(LocationTagged<string> signature, bool headerComplete)
    {
      this.Signature = signature;
      this.HeaderComplete = headerComplete;
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Gets or sets the signature for this code.</summary>
    /// <returns>The signature for this code.</returns>
    public LocationTagged<string> Signature { get; private set; }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Gets or sets the footer for this code.</summary>
    /// <returns>The footer for this code.</returns>
    public LocationTagged<string> Footer { get; set; }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Gets or sets a value indicating whether the header for this code is complete.</summary>
    /// <returns>true if the header for this code is complete; otherwise, false.</returns>
    public bool HeaderComplete { get; private set; }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Generates a block before the code.</summary>
    /// <param name="target">The block to generate.</param>
    /// <param name="context">The code generator context.</param>
    public override void GenerateStartBlockCode(Block target, CodeGeneratorContext context)
    {
      this._writer = context.CreateCodeWriter();
      string snippet = context.BuildCodeString((Action<CodeWriter>) (cw => cw.WriteHelperHeaderPrefix(context.Host.GeneratedClassContext.TemplateTypeName, context.Host.StaticHelpers)));
      this._writer.WriteLinePragma(context.GenerateLinePragma(this.Signature.Location, snippet.Length, this.Signature.Value.Length));
      this._writer.WriteSnippet(snippet);
      this._writer.WriteSnippet((string) this.Signature);
      if (this.HeaderComplete)
        this._writer.WriteHelperHeaderSuffix(context.Host.GeneratedClassContext.TemplateTypeName);
      this._writer.WriteLinePragma((CodeLinePragma) null);
      if (this.HeaderComplete)
      {
        this._writer.WriteReturn();
        this._writer.WriteStartConstructor(context.Host.GeneratedClassContext.TemplateTypeName);
        this._writer.WriteStartLambdaDelegate("__razor_helper_writer");
      }
      this._statementCollectorToken = context.ChangeStatementCollector(new Action<string, CodeLinePragma>(this.AddStatementToHelper));
      this._oldWriter = context.TargetWriterName;
      context.TargetWriterName = "__razor_helper_writer";
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Generates a block after the code.</summary>
    /// <param name="target">The block to generate.</param>
    /// <param name="context">The code generator context.</param>
    public override void GenerateEndBlockCode(Block target, CodeGeneratorContext context)
    {
      this._statementCollectorToken.Dispose();
      if (this.HeaderComplete)
      {
        this._writer.WriteEndLambdaDelegate();
        this._writer.WriteEndConstructor();
        this._writer.WriteEndStatement();
      }
      if (this.Footer != (LocationTagged<string>) null && !string.IsNullOrEmpty(this.Footer.Value))
      {
        this._writer.WriteLinePragma(context.GenerateLinePragma(this.Footer.Location, 0, this.Footer.Value.Length));
        this._writer.WriteSnippet((string) this.Footer);
        this._writer.WriteLinePragma();
      }
      this._writer.WriteHelperTrailer();
      context.GeneratedClass.Members.Add((CodeTypeMember) new CodeSnippetTypeMember(this._writer.Content));
      context.TargetWriterName = this._oldWriter;
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Determines whether the specified object is equal to the current object.</summary>
    /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
    /// <param name="obj">The object to compare to.</param>
    public override bool Equals(object obj) => obj is HelperCodeGenerator helperCodeGenerator && base.Equals((object) helperCodeGenerator) && this.HeaderComplete == helperCodeGenerator.HeaderComplete && object.Equals((object) this.Signature, (object) helperCodeGenerator.Signature);

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Gets the hash code for the current instance.</summary>
    /// <returns>The hash code for the current instance.</returns>
    public override int GetHashCode() => HashCodeCombiner.Start().Add(base.GetHashCode()).Add((object) this.Signature).CombinedHash;

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Returns a string representation of the current instance.</summary>
    /// <returns>A string representation of the current instance.</returns>
    public override string ToString() => "Helper:" + this.Signature.ToString("F", (IFormatProvider) CultureInfo.CurrentCulture) + ";" + (this.HeaderComplete ? "C" : "I");

    private void AddStatementToHelper(string statement, CodeLinePragma pragma)
    {
      if (pragma != null)
        this._writer.WriteLinePragma(pragma);
      this._writer.WriteSnippet(statement);
      this._writer.InnerWriter.WriteLine();
      if (pragma == null)
        return;
      this._writer.WriteLinePragma();
    }
  }
}
